import { mapGetters, mapActions } from 'vuex';
import {
  themeList,
  addCss,
  removeAllCss,
  getReadTimeByMinute,
} from '@/utils/book';
import {
  getBookmark,
  saveLocation,
  getBookShelf,
  saveBookShelf,
} from '@/utils/localStorage';
import { gotoBookDetail, appendAddToShelf } from '@/utils/store';
import { shelf } from '@/api/store';

// 书架的Mixin
export const storeShelfMixin = {
  computed: {
    ...mapGetters([
      'offsetY',
      'isEditMode',
      'shelfList',
      'shelfSelected',
      'shelfTitleVisible',
      'shelfCategory',
      'currentType',
    ]),
  },
  methods: {
    ...mapActions([
      'setOffsetY',
      'setIsEditMode',
      'setShelfList',
      'setShelfSelected',
      'setShelfTitleVisible',
      'setShelfCategory',
      'setCurrentType',
    ]),
    showBookDetail(book) {
      gotoBookDetail(this, book);
    },
    // 得到书架列表,通过API调用
    getShelfList() {
      let shelfList = getBookShelf();
      if (!shelfList) {
        shelf().then((response) => {
          if (
            response.status === 200 &&
            response.data &&
            response.data.bookList
          ) {
            shelfList = appendAddToShelf(response.data.bookList);
            saveBookShelf(shelfList);
            this.setShelfList(shelfList);
          }
        });
      } else {
        this.setShelfList(shelfList);
      }
    },
  },
};

export const storeHomeMixin = {
  computed: {
    ...mapGetters(['offsetY', 'hotSearchOffsetY', 'flapCardVisible']),
  },
  methods: {
    ...mapActions(['setOffsetY', 'setHotSearchOffsetY', 'setFlapCardVisible']),
    showBookDetail(book) {
      gotoBookDetail(this, book);
    },
  },
};

export const ebookMixin = {
  computed: {
    ...mapGetters([
      'fileName',
      'menuVisible',
      'settingVisible',
      'defaultFontSize',
      'defaultFontFamily',
      'fontFamilyVisible',
      'defaultTheme',
      'bookAvailable',
      'progress',
      'section',
      'isPaginating',
      'currentBook',
      'navigation',
      'cover',
      'metadata',
      'paginate',
      'pagelist',
      'offsetY',
      'isBookmark',
    ]),
    themeList() {
      return themeList(this);
    },
    // *获取当前章节名
    getSectionName() {
      /*      if (this.section) {
            const sectionInfo = this.currentBook.section(this.section);
            if (
              sectionInfo &&
              sectionInfo.href &&
              this.currentBook &&
              this.currentBook.navigation
            ) {
              return this.currentBook.navigation.get(sectionInfo.href).label;
            }
          } */
      return this.section ? this.navigation[this.section].label : '';
    },
  },
  methods: {
    ...mapActions([
      'setMenuVisible',
      'setFileName',
      'setSettingVisible',
      'setDefaultFontSize',
      'setDefaultFontFamily',
      'setFontFamilyVisible',
      'setDefaultTheme',
      'setBookAvailable',
      'setProgress',
      'setSection',
      'setIsPaginating',
      'setCurrentBook',
      'setNavigation',
      'setCover',
      'setMetadata',
      'setPaginate',
      'setPagelist',
      'setOffsetY',
      'setIsBookmark',
    ]),
    // *初始化全局样式
    initGlobalStyle() {
      removeAllCss();
      switch (this.defaultTheme) {
        case 'Default':
          addCss(`${process.env.VUE_APP_RES_URL}/theme/theme_default.css`);
          break;
        case 'Eye':
          addCss(`${process.env.VUE_APP_RES_URL}/theme/theme_eye.css`);
          break;
        case 'Gold':
          addCss(`${process.env.VUE_APP_RES_URL}/theme/theme_gold.css`);
          break;
        case 'Night':
          addCss(`${process.env.VUE_APP_RES_URL}/theme/theme_night.css`);
          break;
        default:
          addCss(`${process.env.VUE_APP_RES_URL}/theme/theme_default.css`);
          break;
      }
    },
    // *刷新进度条位置
    refreshLocation() {
      const currentLocation = this.currentBook.rendition.currentLocation();
      if (currentLocation && currentLocation.start) {
        const startCfi = currentLocation.start.cfi;
        const progress = this.currentBook.locations.percentageFromCfi(startCfi);
        this.setProgress(Math.floor(progress * 100));
        this.setSection(currentLocation.start.index);
        saveLocation(this.fileName, startCfi);
        const bookmark = getBookmark(this.fileName);
        if (bookmark) {
          if (bookmark.some((item) => item.cfi === startCfi)) {
            this.setIsBookmark(true);
          } else {
            this.setIsBookmark(false);
          }
        } else {
          this.setIsBookmark(false);
        }
        if (this.pageList) {
          const totalPage = this.pageList.length;
          const currentPage = currentLocation.start.location;
          if (currentPage && currentPage > 0) {
            this.setPaginate(currentPage + '/' + totalPage);
          } else {
            this.setPaginate('');
          }
        } else {
          this.setPaginate('');
        }
      }
    },

    // *渲染函数
    display(target, callBack) {
      if (target) {
        this.currentBook.rendition.display(target).then(() => {
          this.refreshLocation();
          if (callBack) callBack();
        });
      } else {
        this.currentBook.rendition.display().then(() => {
          this.refreshLocation();
          if (callBack) callBack();
        });
      }
    },
    // *隐藏标题栏和菜单栏
    hideTitleAndMenu() {
      // this.$store.dispatch('setMenuVisible', false);
      this.setMenuVisible(false);
      this.setSettingVisible(-1);
      this.setFontFamilyVisible(false);
    },
    // *获取阅读时间文本
    getReadTimeText() {
      return this.$t('book.haveRead').replace(
        '$1',
        getReadTimeByMinute(this.fileName)
      );
    },
  },
};
